define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    


    $.widget('rk.wrExportPopupCtrl', $.rk.oaDialogCtrl, {
        _create: function() {
            var me = this;
            var elem = me.element;

            var html = require('oa/tmpl/workreport/asset/template_popup-export.tpl');
            elem.html(rk.templateText(html));
        },
        _init: function() {
            var me = this;
            var elem = me.element;

            var viewType = me.options.viewType;
            var belongTypeId = me.options.belongTypeId;

            var startDay = moment().startOf('month');
            var endDay = moment();

            elem.find("#exportStartDate").val(startDay.format('YYYY-MM-DD')).datepicker().attr("readOnly", true);
            elem.find("#exportEndDate").val(endDay.format('YYYY-MM-DD')).datepicker().attr("readOnly", true);

            if (viewType != 'mine') {
                elem.find('li.select_users').show();

                if (viewType == 'subordinate') {
                    elem.find('li.select_departments').show();

                }
            }
            elem.find("#subUser").multipeopleselector({});
            $('#subDepartment').multitreeselector({
                width: 250,
                source: '/json/crm_dimension/dim-tree.action?belongId=' + 25 + '&dimType=' + 1
            });
            $('#subDepartment').multitreeselector('val', []); //清空数据

            //报告类型
            var belongtypes = workreportService.getBelongTypes();
            $('#selBelongTypeId').find('option').remove();
            $.each(belongtypes, function(i, bt) {
                $('#selBelongTypeId').append('<option ' + (belongTypeId == bt.id ? 'selected' : '') + ' value="' + bt.id + '">' + rk.htmlEscape(bt.typeName) + '</option>');
            });
            $('#selBelongTypeId').selectmenu();

            var submitForm = elem.find("#exportReportForm");

            //关闭按钮
            me.getBtn('cancel').click(function() {
                rk.closeDialog();
            });
            //导出按钮
            me.getBtn('export').click(function() {
                var start = $.trim($('#exportStartDate').val());
                var end = $.trim($('#exportEndDate').val());
                if (start == '' || end == '') {
                    return;
                }
                if (start > end) {
                    return;
                }
                end = moment(end);
                end.hours(0);
                end.minutes(0);
                end.seconds(0);
                end.add(1, 'day');
                var startMs = moment(start).unix() * 1000;
                var userIds = [];
                var usersInput = elem.find("#subUser")?elem.find("#subUser").multipeopleselector("val"):'';
                if (usersInput) {
                    $.each(usersInput, function(idx, user) {
                        userIds.push(user.value);
                    });
                }
                var endMs = moment(end).unix() * 1000;
                var users = userIds.join(",");

                var url;
                if (viewType == 'mine')
                    url = '/json/oa_workreport/export-my-submitted.action';
                else if (viewType == 'received')
                    url = '/json/oa_workreport/export-to-myself.action';
                else if (viewType == 'subordinate')
                    url = '/json/oa_workreport/export-group.action';

                submitForm.attr('action', url);
                submitForm.find('input[name="belongTypeId"]').val($('#selBelongTypeId').val());
                submitForm.find('input[name="search.startTime"]').val(startMs);
                submitForm.find('input[name="search.endTime"]').val(endMs);

                var departments = $('#subDepartment') ? $('#subDepartment').multitreeselector('val') : '';
                var departmentsInput = submitForm.find('input[name="search.departIds"]');
                if (departments && departments.length > 0) {
                    var vals = [];
                    for (var i = 0, len = departments.length; i < len; i++) {
                        var o = departments[i];
                        vals.push(o.value);
                    }
                    departmentsInput.val(vals.join(','));
                } else {
                    departmentsInput.remove();
                }

                var userIdsInput = submitForm.find('input[name="search.userIds"]');
                if (viewType == 'mine') {
                    userIdsInput.remove();
                } else {
                    if (users) {
                        userIdsInput.val(users);
                    } else {
                        userIdsInput.remove();
                    }
                }
                submitForm.submit();
                $.closePopupLayer();

            });
        }
    });

});
